<?php

namespace App\Http\Controllers;

use App\Http\Lib\ResBase;
use App\Models\Files;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;

class EChartController extends Controller
{
    //获取总数据数
    public function countFile(){
        $count = Files::count();
        return ResBase::ApiRes(200,$count);
    }
    //获取各种类型数据占比
    public function getPercent(){
        $all = Files::count();
        $pdf = Files::where('mime','application/pdf')->count();
        $word = Files::where('mime','application/msword')
            ->orWhere('mime','application/vnd.openxmlformats-officedocument.wordprocessingml.document')
            ->orWhere('mime','application/rtf')
            ->orWhere('mime','atext/html')
            ->orWhere('mime','text/plain')
            ->count();
        $excel = Files::where('mime','application/vnd.ms-excel')
            ->orWhere('mime','application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
            ->count();
        $view = Files::where('mime','image/jpeg')
            ->orWhere('mime','image/png')
            ->orWhere('mime','image/vnd.wap.wbmp')
            ->orWhere('mime','image/vnd.wap.wbmp')
            ->orWhere('mime','video/x-msvideo')
            ->orWhere('mime','video/mpeg')
            ->orWhere('mime','video/quicktime')
            ->orWhere('mime','audio/wav')
            ->orWhere('mime','audio/mpeg')
            ->orWhere('mime','audio/mpeg')
            ->count();
//        if(!$pdf || !$word || !$excel || $view){
//            return ResBase::ApiRes(400,'统计失败','failed');
//        } else {
//            return ResBase::ApiRes(200,['pdf'=>$pdf,'word'=>$word,'excel'=>$excel,'view'=>$view]);
//        }
        $pdfp = $pdf/$all;
        $wordp = $word/$all;
        $excelp = $excel/$all;
        $viewp = $view/$all;
//        return ResBase::ApiRes(200,['pdf'=>$pdfp,'word'=>$wordp,'excel'=>$excelp,'view'=>$viewp]);
        return ResBase::ApiRes(200,[['value'=>$pdf,'name'=>'pdf类文件'],['value'=>$word,'name'=>'word类文件'],['value'=>$excel,'name'=>'表格类文件'],['value'=>$view,'name'=>'影像类文件']]);
    }


    //获取昨日长传量
    public function getYesterday(){
        // 获取昨天的日期
        $yesterday = Carbon::yesterday()->format('Y-m-d');
        $count = Files::whereDate('created_at', $yesterday)->count();
        if($count === 0 ){
            return ResBase::ApiRes(201,'昨日暂无新文件上传');
        } else {
            return ResBase::ApiRes(200,['count'=>$count]);
        }
    }

    //获取上传最多用户
    public function getMost(){
        $result = Files::select('uploader')
            ->selectRaw('COUNT(*) as count')
            ->groupBy('uploader')
            ->orderBy('count', 'desc')
            ->first();
        //dd($result);
        if ($result) {
            $mostFrequentValue = $result->uploader;
            $occurrenceCount = $result->count;
            // 这里的 $mostFrequentValue 就是出现次数最多的值
            // $occurrenceCount 是该值出现的次数
            return ResBase::ApiRes(200,['uploader'=>$mostFrequentValue,'value'=>$occurrenceCount]);
        } else {
            // 未找到数据
            return ResBase::ApiRes(400,'未查询到数据','failed');
        }
    }
}
